#include "../server.hpp"

class EchoServer
{
public:
    EchoServer(int port):_server(port)
    {
        _server.SetThreadLoopCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectCallBack(std::bind(&EchoServer::onConnect,this,std::placeholders::_1));
        _server.SetCloseCallBack(std::bind(&EchoServer::onClose,this,std::placeholders::_1));
        _server.SetMessageCallBack(std::bind(&EchoServer::onMessage,this,std::placeholders::_1,std::placeholders::_2));
    }
    void Start() { _server.Start(); }
private:
    void onMessage(const ConnectionPtr &conn,Buffer *buffer)
    {
        size_t readable = buffer->ReadAbleSize();
        if (readable > 0) {
            std::string msg = buffer->ReadStringAndPop(readable);
            // DLOG("收到数据: %s", msg.c_str());
            std::string reply = "你好: ";
            conn->Send(reply.c_str(), reply.size());
            conn->ShutDown();
            //conn->ShutDown();
            //DLOG("发送成功");
        }
    }
    void onClose(const ConnectionPtr &conn)
    {
        DLOG("CLOSE CONNECTION:%p", conn.get());
    }
    void onConnect(const ConnectionPtr &conn)
    {
        DLOG("NEW CONNECTION:%p", conn.get());
    }
private:
    TcpServer _server;
};